package org.logan.sample.web.main.action.common;

import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.logan.sample.base.utils.CastToBean;
import org.logan.sample.db.model.SysUser;
import org.logan.sample.web.base.common.Pager;

/**
 * @Desc：基础控制层
 * @Title：BaseController
 * @address: http://www.liveneo.com.cn/
 * @author: zhaoyy
 * @version: 1.0 
 * @date: 2016年11月2日 下午3:42:14
 */
public class BaseController {
    protected static Log   logger  = LogFactory.getLog(BaseController.class);
    protected final String ERROR   = "e";
    protected final String RESULT  = "r";
    protected final String MESSAGE = "m";

    /**
     * @Desc: 重定向页面
     * @param url
     * @return
     */
    protected String redirect(String url) {
        return "redirect:" + url;
    }

    /**
     * @Desc: 跳转页面
     * @param url
     * @return
     */
    protected String forward(String url) {
        return "forward:" + url;
    }

    /**
     * 获取resultMap对象
     * @return
     */
    protected Map<String, Object> getResultMap() {
        return new HashMap<String, Object>();
    }

    /**
     * 创建分页对象
     * @param request
     * @return
     */
    public Pager createPager(HttpServletRequest request) {
        Pager pager = null;
        String szPageSize = request.getParameter("rows");
        String szPageNo = request.getParameter("page");
        String sort = request.getParameter("sort");
        String order = request.getParameter("order");
        Integer pageSize = StringUtils.isNotEmpty(szPageSize) ? Integer.valueOf(szPageSize) : null;
        Integer pageNo = StringUtils.isNotEmpty(szPageNo) ? Integer.valueOf(szPageNo) : null;
        pager = new Pager(pageSize, pageNo);
        if (StringUtils.isNotEmpty(sort)) pager.setSort(sort);
        if (StringUtils.isNotEmpty(order)) pager.setOrder(order);
        return pager;
    }

    /**
     * @Desc: 请求参数转换
     * @Author: zhaoyy 2016年11月2日 下午5:41:08
     * @param cls
     * @param request
     * @param field
     * @return
     */
    protected <T> T getRequestParams(Class<T> cls, HttpServletRequest request, String field) {
        String value = request.getParameter(field);
        value = StringUtils.isBlank(value) ? null : value;
        return CastToBean.typeCast(cls, value);
    }

    /**
     * 封装页面表格数据
     * @param pager
     * @return
     */
    public Map<String, Object> getGridData(Pager pager) {
        Map<String, Object> result = getResultMap();
        result.put("total", pager.getTotalRecord());
        result.put("rows", pager.getResult());
        result.put("footer", pager.getFooter());
        return result;
    }

    /**
     * 获取系统登录用户
     * @param request
     * @return
     */
    public static SysUser getUser() {
        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated()) {
            return (SysUser) subject.getPrincipal();
        }
        return null;
    }
}
